package org.grottarossa.ui.core.container;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.GregorianCalendar;


import org.grottarossa.template.data.enums.EnumDataType;
import org.grottarossa.template.data.pojo.table.Cell;
import org.grottarossa.template.data.pojo.table.Record;
import org.grottarossa.template.data.pojo.table.Table;

import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;

public class TableContainer extends IndexedContainer
{
	private static final long serialVersionUID = 1L;

	/*** La tabella dati rappresentata dal container ***/
	protected Table m_dataTable;

	private String m_storagePropertyId;

	public TableContainer(String storagePropertyId)
	{
		m_storagePropertyId = storagePropertyId;
	}

	public TableContainer()
	{
		m_storagePropertyId = null;
	}

	/***
	 * Imposta i dati sul container. E' necessario chiamarla prima di utilizzare il container.
	 * @param data
	 */
	public void setDataTable(Table data)
	{
		this.m_dataTable = data;

		if(data == null)
			return;

		if ( this.m_dataTable.isEmpty() )
			createTableHeader();
		else
		{
			createTableHeader();
			populateRecords();
		}
	}

	/***
	 * Genera l'intestazione del container
	 */
	private void createTableHeader()
	{
		ArrayList<Cell> headerCells = m_dataTable.getHeader().getHeaderCells();

		if(headerCells != null)
		{
			for(int i=0;i<headerCells.size();i++)
			{
				Cell cell = headerCells.get(i);

				//modify by mfarina on 09/03/2011
				//Probabilmente la tabella � vuota per cui non � stato valorizzato il datatype.
				if ( cell.getDataType() == null )
					addContainerProperty(cell.getValue(), String.class, null);
				else
				{
					if(cell.getDataType().isBoolean())
						addContainerProperty(cell.getValue(), Boolean.class, null);
					else if(cell.getDataType().isInteger())
						addContainerProperty(cell.getValue(), Integer.class, null);
					else if(cell.getDataType().isString())
						addContainerProperty(cell.getValue(), String.class, null);
					else if(cell.getDataType().isLongText())
						addContainerProperty(cell.getValue(), String.class, null);
					else if(cell.getDataType().isCalendar())
						addContainerProperty(cell.getValue(), GregorianCalendar.class, null);
					else if(cell.getDataType().isBigDecimal())
						addContainerProperty(cell.getValue(), BigDecimal.class, null);
					else if(cell.getDataType().isDuration())
						addContainerProperty(cell.getValue(), BigDecimal.class, null);
				}
			}
		}
	}

	/***
	 * Popola il contenuto del container
	 */
	protected void populateRecords()
	{
		ArrayList<Record> records = m_dataTable.getRecords();
		ArrayList<Cell> headerCells = m_dataTable.getHeader().getHeaderCells();

		for(int i=0;i<records.size();i++)
		{
			addRow(records.get(i), headerCells);
		}
	}



	/***
	 * Aggiunge una riga al container
	 * @param record
	 * @param headerCells
	 */
	protected void addRow(Record record, ArrayList<Cell> headerCells)
	{
		ArrayList<Cell>cellList = record.getCells();

		Item it;

		if(m_storagePropertyId != null)
		{
			Cell c = record.getCell(m_storagePropertyId);
			Object value = EnumDataType.convertType(c.getDataType(), c.getValue().toString());
			it = (Item)addItem(value);
		}
		else
		{
			Object obj = addItem();
			it = getItem(obj);
		}

		for(int j=0;j<cellList.size();j++)
		{
			it.getItemProperty(headerCells.get(j).getValue()).setValue(cellList.get(j).getValue());
		}
	}

	/***
	 * Restituisce la tabella dati attualmente rappresentata dal container
	 * @return
	 */
	public Table getDataTable()
	{
		return m_dataTable;
	}
}
